package cn.android.support.v7.lib.sin.crown.dialog;

import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.R;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.view.RollerView;
import cn.android.support.v7.lib.sin.crown.view.WheelTurns;

/**
 * 滾輪彈出窗【升級版】
 * Created by 彭治铭 on 2018/1/7.
 */

public class RollerLvDialog extends BaseDialog {

    private static RollerLvDialog rollerDialog;

    public static RollerLvDialog getInstance(Activity activity) {
        if (isfinish(activity, rollerDialog)) {
            rollerDialog = new RollerLvDialog(activity);
        }
        return rollerDialog;
    }

    private RollerLvDialog(Activity activity) {
        init(activity, R.layout.crown_dialog_rollerlv);
        window.setWindowAnimations(R.style.crown_window_bottom);
        initEx();
    }


    Button buttonComplete;
    RollerView roller;


    public Button crown_btn_cancel, crown_btn_info;

    //设置右边完成按钮信息
    public RollerLvDialog setBtnCompleteText(String info) {
        buttonComplete.setText(info);
        return this;
    }

    //设置右边完成按钮字体颜色
    public RollerLvDialog setBtnCompleteColor(int color) {
        buttonComplete.setTextColor(color);
        return this;
    }

    //设置中间按钮信息
    public RollerLvDialog setBtnInfoText(String info) {
        crown_btn_info.setText(info);
        return this;
    }

    //设置中间按钮字体颜色
    public RollerLvDialog setBtnInfoColor(int color) {
        crown_btn_info.setTextColor(color);
        return this;
    }


    //设置取消按钮信息
    public RollerLvDialog setBtnCancelText(String info) {
        crown_btn_cancel.setText(info);
        if (info == null || info.trim().equals("")) {
            crown_btn_cancel.setOnClickListener(null);
        } else {
            crown_btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
        return this;
    }

    //设置取消按钮字体颜色
    public RollerLvDialog setBtnCancelColor(int color) {
        crown_btn_cancel.setTextColor(color);
        return this;
    }


    //设置数据是否循环显示
    public RollerLvDialog setCircle(Boolean circle) {
        roller.setCyclic(circle);
        return this;
    }

    private void initEx() {
        crown_btn_info = (Button) findViewById(R.id.crown_btn_info);
        crown_btn_cancel = (Button) findViewById(R.id.crown_btn_cancel);
        ProportionUtils.getInstance().adapterButton(crown_btn_cancel);
        ProportionUtils.getInstance().adapterButton(crown_btn_info);
        crown_btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    @Override
    protected void initUI() {
        buttonComplete = (Button) findViewById(R.id.crown_btn_complete);
        roller = (RollerView) findViewById(R.id.roller);
        buttonComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (positiveListener != null) {
                    positiveListener.onItemSelect(roller.getCurrentItemValue(),roller.getCurrentPostion());
                }
                dismiss();
            }
        });
    }

    @Override
    protected void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(activity, findViewById(R.id.crown_roller_dialog));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.crown_startLine));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.crown_endLine));
        ProportionUtils.getInstance().adapterButton(buttonComplete);
    }

    @Override
    protected boolean isAutoAdapterUI() {
        return false;
    }

    @Override
    protected boolean isStatus() {
        return true;
    }

    @Override
    protected boolean isTransparent() {
        return false;
    }

    @Override
    protected boolean isLocked() {
        return false;
    }

    @Override
    protected boolean isDismiss() {
        return true;//消失
    }

    @Override
    protected boolean isRecycel() {
        return false;
    }

    @Override
    protected boolean isAutoShow() {
        return false;
    }

    @Override
    protected void listener() {

    }

    @Override
    protected void recycleView() {
    }


    /**
     * @param items 數據集合
     * @param count item显示个数
     * @return
     */
    public RollerLvDialog setItems(List<String> items, int count) {
        roller.setCount(count);
        roller.setItems(items);
        return this;
    }

    public interface PositiveListener {
        //原始数据，和下标
        void onItemSelect(String item, int position);
    }

    PositiveListener positiveListener;

    //确定按钮监听事件
    public RollerLvDialog setPositiveListener(PositiveListener positiveListener) {
        this.positiveListener = positiveListener;
        return this;
    }

    //滚轮监听事件
    public RollerLvDialog setItemSelectListener(RollerView.ItemSelectListener itemSelectListener) {
        roller.setItemSelectListener(itemSelectListener);
        return this;
    }

    //根据下标，选中指定item
    public RollerLvDialog selectIem(int positon) {
        roller.setCurrentPostion(positon);
        return this;
    }

    public static void setRollerDialog(RollerLvDialog rollerDialog) {
        RollerLvDialog.rollerDialog = rollerDialog;
    }

    public void setButtonComplete(Button buttonComplete) {
        this.buttonComplete = buttonComplete;
    }

    public void setRoller(RollerView roller) {
        this.roller = roller;
    }

    public void setCrown_btn_cancel(Button crown_btn_cancel) {
        this.crown_btn_cancel = crown_btn_cancel;
    }

    public void setCrown_btn_info(Button crown_btn_info) {
        this.crown_btn_info = crown_btn_info;
    }

}
